home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Macintosh / Development & Resource Tools / MacsBug6.5.3.sit / MacsBug 6.5.3 / Building dcmds / C Samples / Printf.c < prev    next >
Text File  |  1996-02-22  |  4KB  |  133 lines

  1. /*
  2.     File:        Printf.c
  3.  
  4.     Contains:    A dcmd that supports the C concept of printf.
  5.  
  6.     Written by:    JM3 = Jim Murphy
  7.                 DAL = Dave Lyons
  8.                 sad = Scott Douglass
  9.  
  10.     Copyright:    © 1989, 1994-1996 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.     Change History (most recent first):
  13.  
  14.          <5>   25-Jan-96    JM3        Let's try that again. We supply Put.c as a library, so it
  15.                                     doesn't need compiled.
  16.          <4>   25-Jan-96    JM3        Fixed the warning in <3> again. The latest universal headers
  17.                                     re-introduced this. Updated the sample build commands to be
  18.                                     current.
  19.          <3>     3/26/95    DAL        Included <:Public:Strings.h> to avoid p2cstr warning (should fix
  20.                                     header file names). Used new dcmdFillVersion and dcmdFillString
  21.                                     macros.
  22.          <2>   10-Dec-94    JM3        Updated for new format 3 dcmd requirements.
  23.          <1>     2/16/89    sad        written
  24.  
  25. */
  26.  
  27. /*    This is mostly to show how dcmds can use the standard C library.
  28.  
  29.     The following MPW commands will build the dcmd and copy it to the "Debugger Prefs" file 
  30.     in the System folder. The dcmd's name in MacsBug will be the name of the file built by
  31.     the Linker.
  32.  
  33.     C Printf.c
  34.  
  35.     Link -o Printf -sg Main=STDCLIB,STDIO,SANELIB dcmdGlue.a.o Put.c.o Printf.c.o ∂
  36.         "{Libraries}Runtime.o" "{CLibraries}StdCLib.o" "{Libraries}Interface.o" ∑∑ dev:null
  37.     BuildDcmd Printf 196 -format3
  38.     Echo 'include "Printf";'    |    Rez -a -o "{SystemFolder}Debugger Prefs"
  39. */
  40.  
  41. #include <Types.h>
  42.  
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <TextUtils.h>
  46. #include <Memory.h>
  47.  
  48. #include "dcmd.h"
  49. #include "put.h"
  50.  
  51. pascal void CommandEntry(dcmdBlock* paramPtr)
  52. {
  53.  
  54.     static const char usageStr[] = "\p\"format\" arg...";
  55.  
  56.     switch (paramPtr->request)
  57.     {
  58.         case dcmdInit:
  59.             break;
  60.  
  61.         case dcmdHelp:
  62.             dcmdDrawLine("\pDisplays the arguments according to the format (no floating point).");
  63.             break;
  64.  
  65.         case dcmdGetInfo:
  66.             dcmdFillVersion(paramPtr, 0x03008000);    // version 3.0
  67.             dcmdFillString(paramPtr, usageStr, usageStr);
  68.             break;
  69.  
  70.         case dcmdDoIt:
  71.         {
  72.             Str255 formatstring;
  73.             unsigned char* formatp = formatstring;
  74.             Str255 formattedstring;
  75.  
  76.             dcmdSwapWorlds();            // not really necessary because we don’t access machine state
  77.  
  78.             (void)dcmdGetNextParameter(formatstring);
  79.  
  80.             p2cstr(formatstring);
  81.  
  82.             while (*formatp != '\0')
  83.             {
  84.                 char aformatspec[256];
  85.                 size_t speclength;
  86.                 long arg;
  87.                 Boolean ok;
  88.  
  89.                 // Find the next format spec (i.e. %d) and print out anything before it
  90.             
  91.                 char* nextp = strchr(formatp, '%');
  92.                 if (nextp == nil) nextp = formatp + strlen(formatp);
  93.  
  94.                 PutBytesTo(formatp, nextp - formatp, 0);
  95.                 formatp = nextp;
  96.  
  97.                 // Find the length of the format spec
  98.             
  99.                 speclength = strcspn(formatp + 1, "diouxXcspP%") + 2;    // we don’t do "feEgGn"
  100.                 memcpy(aformatspec, formatp, speclength);
  101.                 aformatspec[speclength] = '\0';
  102.                 (void)dcmdGetNextExpression(&arg, &ok);
  103.                 if (!ok) break;
  104.  
  105.                 sprintf(formattedstring, aformatspec, arg);
  106.                 PutCStr(formattedstring);
  107.                 formatp = formatp + speclength;
  108.             }
  109.             PutLine();
  110.  
  111.             dcmdSwapWorlds();
  112.         }
  113.             break;
  114.  
  115.         // Version 3 and newer dcmds must quietly ignore requests we don't recognize.
  116.     
  117.         default:
  118.             break;
  119.     }
  120.  
  121. } // CommandEntry
  122.  
  123. /*
  124.     the following are stubs to override the C library routines so that the dcmd isn’t
  125.     so big.
  126. */
  127.  
  128. size_t fwrite (const void *, size_t, size_t, FILE *) { return 0; }        // wont’t actually be called by sprintf
  129. _flsbuf() {}    // wont’t actually be called by sprintf
  130.  
  131. fcvt() {}        // used only for floating point %f, etc.
  132. ecvt() {}        // used only for floating point %e, etc.
  133.